package com.yt.product.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.product.common.config.YmlConfig;
import com.yt.product.mapper.ProductInfoMapper;
import com.yt.product.models.bo.ProductInfoSaveBO;
import com.yt.product.models.dto.ProductCategoryDTO;
import com.yt.product.models.dto.ProductInfoDTO;
import com.yt.product.models.dto.SupplierInfoDTO;
import com.yt.product.models.service.IProductInfoService;
import com.yt.product.models.vo.ProductInfoVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品信息控制器
 *
 * @author zhou
 * @Date 2025-09-05 11:31:42
 */
@DubboService
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfoDTO> implements IProductInfoService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private ProductInfoMapper productInfoMapper;
    @Resource
    private SupplierInfoServiceImpl supplierInfoService; // 注入供应商服务
    @Resource
    private ProductCategoryServiceImpl productCategoryService; // 注入产品类别服务

    @Override
    public boolean add(ProductInfoSaveBO saveBO) {
        ProductInfoDTO dto = new ProductInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<ProductInfoVO> queryList() {
        List<ProductInfoVO> productInfoVOS = baseMapper.query();
        return productInfoVOS;
    }

    @Override
    public ProductInfoDTO saveProduct(ProductInfoSaveBO bo) {
        ProductInfoDTO dto = new ProductInfoDTO();
        BeanUtils.copyProperties(bo, dto);
        //主键id
        dto.setId(ymlConfig.getPkId());
        //分类id
        Wrapper<ProductCategoryDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("category_name", bo.getCategoryName());
        ProductCategoryDTO productCategoryDTO = productCategoryService.selectOne(wrapper);
        if (productCategoryDTO == null) {
            dto.setCategoryId(null);
        }
        dto.setCategoryId(productCategoryDTO.getId());
        //供应商id
        Wrapper<SupplierInfoDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("supplier_name", bo.getSupplierName());
        SupplierInfoDTO sup = supplierInfoService.selectOne(wrapper1);
        if (sup == null) {
            dto.setSupplierId(null);
        }
        dto.setSupplierId(sup.getId());
        return dto;
    }

    @Override
    public boolean deleteProductById(Long id) {
        // 调用Mapper的deleteById方法（MyBatis-Plus自带该方法，直接删除）
        return productInfoMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateProduct(ProductInfoSaveBO bo) {
        ProductInfoDTO dto = new ProductInfoDTO();
        BeanUtils.copyProperties(bo, dto);
        boolean flag = productInfoMapper.updateById(dto) > 0;
        return flag;
    }

}

